import os
import re
import glob

# 定义脚本路径映射
script_path_mapping = {
    "res://scripts/player.gd": "res://scripts/player/player.gd",
    "res://scripts/player_stats.gd": "res://scripts/player/player_stats.gd",
    "res://scripts/player_stats_ui.gd": "res://scripts/ui/player_stats_ui.gd",
    "res://scripts/player_health_bar.gd": "res://scripts/ui/player_health_bar.gd",
    "res://scripts/monster_health_bar.gd": "res://scripts/ui/monster_health_bar.gd",
    "res://scripts/main_menu.gd": "res://scripts/ui/main_menu.gd",
    "res://scripts/skill_tree_ui.gd": "res://scripts/ui/skill_tree_ui.gd",
    "res://scripts/enemy_base.gd": "res://scripts/enemies/enemy_base.gd",
    "res://scripts/sword_master.gd": "res://scripts/enemies/boss/sword_master.gd",
    "res://scripts/enemy_spawner.gd": "res://scripts/enemies/enemy_spawner.gd",
    "res://scripts/dialog_box.gd": "res://scripts/dialog/dialog_box.gd",
    "res://scripts/dialog_manager.gd": "res://scripts/dialog/dialog_manager.gd",
    "res://scripts/bubble_dialog.gd": "res://scripts/dialog/bubble_dialog.gd",
    "res://scripts/npc.gd": "res://scripts/dialog/npc.gd",
    "res://scripts/inventory.gd": "res://scripts/inventory/inventory.gd",
    "res://scripts/inventory_manager.gd": "res://scripts/inventory/inventory_manager.gd",
    "res://scripts/inventory_slot.gd": "res://scripts/inventory/inventory_slot.gd",
    "res://scripts/inventory_slot_ui.gd": "res://scripts/inventory/inventory_slot_ui.gd",
    "res://scripts/inventory_ui.gd": "res://scripts/inventory/inventory_ui.gd",
    "res://scripts/item.gd": "res://scripts/inventory/item.gd",
    "res://scripts/item_data.gd": "res://scripts/inventory/item_data.gd",
    "res://scripts/item_database.gd": "res://scripts/inventory/item_database.gd",
    "res://scripts/item_database_resource.gd": "res://scripts/inventory/item_database_resource.gd",
    "res://scripts/skill_tree.gd": "res://scripts/skills/skill_tree.gd",
    "res://scripts/sword_wave.gd": "res://scripts/skills/sword_wave.gd",
    "res://scripts/gamedata/skill/skill_database.gd": "res://scripts/skills/database/skill_database.gd",
    "res://scripts/level_up_effect.gd": "res://scripts/effects/level_up_effect.gd",
    "res://scripts/main.gd": "res://scripts/core/main.gd",
    "res://scripts/empty_state_machine.gd": "res://scripts/core/empty_state_machine.gd",
    "res://scripts/language_manager.gd": "res://scripts/core/language_manager.gd",
    "res://enemy_base.gd": "res://scripts/enemies/enemy_base.gd"
}

# 获取所有场景文件
scene_files = glob.glob("scenes/*.tscn") + glob.glob("resources/*.tres")

# 遍历每个场景文件
for scene_file in scene_files:
    print(f"处理文件: {scene_file}")
    
    # 读取文件内容
    with open(scene_file, 'r', encoding='utf-8') as f:
        content = f.read()
    
    # 替换脚本路径
    for old_path, new_path in script_path_mapping.items():
        content = content.replace(f'path="{old_path}"', f'path="{new_path}"')
    
    # 写回文件
    with open(scene_file, 'w', encoding='utf-8') as f:
        f.write(content)
    
    print(f"已更新: {scene_file}")

print("所有场景文件已更新完成！")

# 现在更新脚本文件中的引用
script_files = []
for root, dirs, files in os.walk("scripts"):
    for file in files:
        if file.endswith(".gd"):
            script_files.append(os.path.join(root, file))

# 遍历每个脚本文件
for script_file in script_files:
    print(f"处理脚本: {script_file}")
    
    # 读取文件内容
    with open(script_file, 'r', encoding='utf-8') as f:
        content = f.read()
    
    # 替换脚本引用
    for old_path, new_path in script_path_mapping.items():
        # 移除 res:// 前缀以匹配相对路径引用
        old_rel_path = old_path.replace("res://", "")
        new_rel_path = new_path.replace("res://", "")
        
        # 替换 preload 和 load 函数中的路径
        content = content.replace(f'preload("{old_path}")', f'preload("{new_path}")')
        content = content.replace(f'load("{old_path}")', f'load("{new_path}")')
        content = content.replace(f'preload("{old_rel_path}")', f'preload("{new_rel_path}")')
        content = content.replace(f'load("{old_rel_path}")', f'load("{new_rel_path}")')
    
    # 写回文件
    with open(script_file, 'w', encoding='utf-8') as f:
        f.write(content)
    
    print(f"已更新脚本: {script_file}")

print("所有脚本文件已更新完成！") 